<?php
class Bootstrap_Translator extends Bootstrap_Abstarct {
	const CACHE_TRANSLATOR_NAME = 'zend_translator';
	const DEFAULT_LOCAL_LANG = 'en_US';
	
	static public function build() {
		$config = Zend_Registry::get ( 'config' );
		
		$s = new Zend_Session_Namespace ( 'local' );
		$locale_session = $config->local->session_name;
		$locale_session = empty ( $locale_session ) ? "local_lang" : $locale_session;
		$_lang = (isset ( $s->$locale_session )) ? $s->$locale_session : trim ( $config->local->default );
		
		$_locale = new Zend_Locale ( $_lang );
		Zend_Registry::set ( 'locale', $_locale );
		
		if (Zend_Registry::isRegistered ( 'viewRenderer' )) {
			$viewRenderer = Zend_Registry::get ( 'viewRenderer' );
		} else {
			$viewRenderer = self::loadCache ( self::CACHE_VIEWREBDERER_NAME );
			if (! $viewRenderer instanceof Zend_Controller_Action_Helper_ViewRenderer) {
				
				$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer ( );
				$viewRenderer->setViewSuffix ( $config->view->suffix );
				self::saveCache ( $viewRenderer, self::CACHE_VIEWREBDERER_NAME );
			}
		}
		if (empty ( $viewRenderer ) or ! $viewRenderer instanceof Zend_Controller_Action_Helper_ViewRenderer) {
			throw new Exception ( "PLease check the view renderer settings." );
		}
		
		if (Zend_Registry::isRegistered ( 'Zend_Translate' )) {
			$translator = Zend_Registry::get ( 'Zend_Translate' );
		} else {
			$translatorId = self::CACHE_TRANSLATOR_NAME . $_lang;
			$translator_cache = Config_Xml::isTrue ( $config->local->translator_cache );
			$result = null;
			if ($translator_cache) {
				$result = self::loadCache($translatorId );
			}
			if ($result) {
				$translator = ($result instanceof Zend_Translate) ? $result : unserialize ( $result );
			} else {
				$translator_options = array ('delimiter' => '=' );
				
				$langDir = LANGUAGE_DIR . DIRECTORY_SEPARATOR . $_lang . DIRECTORY_SEPARATOR;
				
				$files = $config->local->translator_files;
				
				if (empty ( $files )) {
					$fileOpration = File_Opration::getInstance ();
					$files = $fileOpration->getAllFiles ( $langDir, "csv" );
				} else {
					$files = explode ( ";", $files );
				}
				$files_count = count ( $files );
				
				if (empty ( $files_count ) or $files_count == 0) {
					throw new Exception ( 'Please check lang-file settings.' );
				}
				
				$translator = new Zend_Translate ( Zend_Translate::AN_CSV, $langDir . $files [0], $_locale, $translator_options );
				
				foreach ( $files as $key => $file ) {
					if ($key == 0) {
						continue;
					}
					$translator->addTranslation ( $langDir . $file, $_locale, $translator_options );
				}
				
				if ($translator_cache) {
					self::saveCache($translator, $translatorId );
				}
			}
			Zend_Registry::set ( 'Zend_Translate', $translator );		
		}
		
		if (! $translator instanceof Zend_Translate) {
			throw new Exception ( "Please check the translator settings." );
		}
		$translator->setLocale ( $_locale );
		
		return $translator;
	}
}